package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author hu
 * @date 2025/5/24
 */
@RequestMapping("/admin/setmeal")
@RestController
@Slf4j
@Api(tags = "套餐相关接口")
public class SetmealController {

    @Resource
    private SetmealService setmealService;


    /**
     * 套餐分页查询
     */
    @GetMapping("/page")
    @ApiOperation("套餐分页查询")
    public Result<PageResult> page(SetmealPageQueryDTO queryDTO) {
        log.debug("分页查询参数: {}", queryDTO);
        PageResult pageResult = setmealService.pageQuery(queryDTO);
        return Result.success(pageResult);
    }

    /**
     * 新增套餐
     */
    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    public Result save(@RequestBody SetmealDTO setmealDTO) {
        log.debug("新增套餐: {}", setmealDTO);
        setmealService.saveSetmeal(setmealDTO);
        return Result.success();
    }

    /**
     * 页面回显
     */
    @GetMapping("/{id}")
    @ApiOperation("页面回显")
    public Result<SetmealVO> getById(@PathVariable Long id) {
        log.debug("页面回显: {}", id);
        SetmealVO setmealVO = setmealService.getById(id);
        return Result.success(setmealVO);
    }

    /**
     * 修改套餐
     */
    @PutMapping
    @ApiOperation("修改套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result<SetmealVO> update(@RequestBody SetmealDTO dto) {
        log.debug("修改套餐: {}", dto);
        setmealService.updateById(dto);
        return Result.success();
    }

    /**
     * 删除套餐
     */
    @DeleteMapping
    @ApiOperation("删除套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result delete(@RequestParam List<Long> ids) {
        log.debug("删除套餐: {}", ids);
        setmealService.deleteSetmeal(ids);
        return Result.success();
    }

    /**
     * 套餐起售or停售
     */
    @PostMapping("/status/{status}")
    @ApiOperation("套餐起售or停售")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result delete(@PathVariable Integer status, Long id) {
        log.debug("套餐起售or停售: {},{}", status, id);
        setmealService.startOrQuit(status, id);
        return Result.success();
    }

}

